/**
 * @author chenwx
 * @description 首页控制类
 * @createTime 2019-11-25 16:53
 */
var indexController = (function () {

    var _className = "indexController"

    var _logGridId = "logGrid",
        _logGrid = null;

    /**
     * 列表初始化
     * @private
     */
    var _initGrid = function () {
        wui.logMethodCalled("_initGrid", _className);

        /*var gridComplete = function(gridId) {
            wui.logMethodCalled("gridComplete", _className);
            // Grid按钮
            $("#grid-box a[action]").each(function(i) {
                var action = $(this).attr("action");
                $(this).click(function(ev) {
                    var ev = ev || window.ev;
                    wui.stopPropagation(ev);
                    wui.logMethodCalled("grid-" + action + ".click");
                });
            });
        }*/

        var gridConfig = {
            url: indexService.urlSettings.pageQuerySyncLog,
            isShowRowNumbers: true,
            isShowCheckBox: false,
            colModel: [
                {
                    label: '日志id', name: 'logId', key: true, hidden: true
                },
                {
                    label: '同步类型', name: 'syncType', width: 50, align: "center",
                    formatter: "select", formatterOption: "user:用户数据;org: 组织数据",
                    sortable: true, search: true, searchoptions: {value:":全部;user:用户数据;org:组织数据"}
                },
                {
                    label: '同步开始时间', name: 'beginTime', width: 50, align: "center",
                    formatter: "dateRange", formatDate: "yyyy-mm-dd hh:ii:ss", sortable: true, search: true
                },
                {
                    label: '同步结束时间', name: 'endTime', width: 50, align: "center",
                    sortable: true, search: false
                },
                {
                    label: '同步结果', name: 'isSuccess', width: 40, align: "center",
                    formatter: "select", formatterOption: "1:成功;0: 失败", sortable: true
                },
                {
                    label: '同步数量', name: 'syncCount', width: 40, align: "center",

                },
                {
                    label: '异常信息', name: 'error', width: 200, align: "left"
                }
            ],
            //onGridComplete : gridComplete,
        }

        _logGrid = wui.grid(_logGridId, gridConfig);
    }

    /**
     * 绑定页面事件
     * @private
     */
    var _bindEvent = function () {
        $(".my-syncBtn").click(function () {
            var load = wui.load();
            var syncType = $(this).attr("synctype");
            var method = "";
            switch (syncType) {
                case "orgFull":
                    method = "execOrgFullSync";
                    break;
                case "orgInc":
                    method = "execOrgIncSync";
                    break;
                case "userFull":
                    method = "execUserFullSync";
                    break;
                case "userInc":
                    method = "execUserIncSync";
                    break;
                default:
                    wui.errorNotice("操作有误");
                    return;
            }

            var success = function (message) {
                wui.closeIndex(load);
                _refreshGrid(message)
            }
            var fail = function(res){
                wui.closeIndex(load);
                wui.errorNotice(res.message);
            }
            indexService[method].call(this, success, fail);
        })
    }

    /**
     * 刷新列表
     * @param message
     * @private
     */
    var _refreshGrid = function (message) {
        if(message){
            wui.successNotice(message);
        }
        _logGrid.refreshData();
    }
    
    return{
        init: function () {
            _initGrid();
            _bindEvent();
        }
    }
})()

$(function () {
    indexController.init();
})